﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.datatables;
using l1cs.server.templates;
using l1cs.server.model;
using System.Threading;

namespace l1cs.server.clientpackets
{
    public class C_Door : ClientBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( C_Door ).FullName );
        private const String _C_DOOR = "[C] C_Door";

        public C_Door( byte[] abyte0, ClientThread client )
            : base( abyte0 )
        {
            int locX = readH();
            int locY = readH();
            int objectId = readD();

            L1PcInstance pc = client.getActiveChar();
            L1DoorInstance door = (L1DoorInstance)L1World.getInstance().findObject( objectId );
            if ( door == null )
            {
                return;
            }

            if ( ( door.getDoorId() >= 5001 && door.getDoorId() <= 5010 ) )
            { // 水晶の洞窟
                return;
            }
            else if ( door.getDoorId() == 6006 )
            { // TIC2F
                if ( door.getOpenStatus() == ActionCodes.ACTION_Open )
                {
                    return;
                }
                if ( pc.getInventory().consumeItem( 40163, 1 ) )
                { 
                    // ゴールデンキー
                    door.open();
                    // TODO 優先度9 タイマースレッド
                    //CloseTimer closetimer = new CloseTimer(door);
                    //closetimer.begin();
                    new Timer( new TimerCallback( CloseTimerSub ), null, 5 * 1000, Timeout.Infinite );
                }
            }
            else if ( door.getDoorId() == 6007 )
            { // TIC2F
                if ( door.getOpenStatus() == ActionCodes.ACTION_Open )
                {
                    return;
                }
                if ( pc.getInventory().consumeItem( 40313, 1 ) )
                {
                    // パールシルバーキー
                    door.open();
                    // TODO 優先度9 タイマースレッド
                    //CloseTimer closetimer = new CloseTimer(door);
                    //closetimer.begin();
                    new Timer( new TimerCallback( CloseTimerSub ), null, 5 * 1000, Timeout.Infinite );
                }
            }
            else if ( !isExistKeeper( pc, door.getKeeperId() ) )
            {
                if ( door.getOpenStatus() == ActionCodes.ACTION_Open )
                {
                    door.close();
                }
                else if ( door.getOpenStatus() == ActionCodes.ACTION_Close )
                {
                    door.open();
                }
            }
        }

        private bool isExistKeeper( L1PcInstance pc, int keeperId )
        {
            if ( keeperId == 0 )
            {
                return false;
            }

            L1Clan clan = L1World.getInstance().getClan( pc.getClanname() );
            if ( clan != null )
            {
                int houseId = clan.getHouseId();
                if ( houseId != 0 )
                {
                    L1House house = HouseTable.getInstance().getHouseTable( houseId );
                    if ( keeperId == house.getKeeperId() )
                    {
                        return false;
                    }
                }
            }
            return true;
        }


        //public class CloseTimer extends TimerTask {

        //    private L1DoorInstance _door;

        //    public CloseTimer(L1DoorInstance door) {
        //        _door = door;
        //    }

        //    override
        public void CloseTimerSub( object state )
        {
            L1DoorInstance _door = (L1DoorInstance)state;
            if ( _door.getOpenStatus() == ActionCodes.ACTION_Open )
            {
                _door.close();
            }
        }

        //    public void begin() {
        //        Timer timer = new Timer();
        //        timer.schedule(this, 5 * 1000);
        //    }
        //}

        override
        public String getType()
        {
            return _C_DOOR;
        }
    }
}
